package com.iyue.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 《星座》实现类
 * 
 * @author ThinkPad
 * @version $Id: Constellation.java,v 1.1 2009/02/16 07:52:57 sunxy Exp $
 */
public class Constellation {

	public static final String[] zodiac = { "猴", "鸡", "狗", "猪", "鼠", "牛", "虎",
			"兔", "龙", "蛇", "马", "羊" };

	public static final String[] constellation = { "水瓶座", "双鱼座", "牡羊座", "金牛座",
			"双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };

	public static final int[] constellationEdgeDay = { 20, 19, 21, 21, 21, 22,
			23, 23, 23, 23, 22, 22 };

	/**
	 * 根据日期获取生肖
	 * 
	 * @return Calendar
	 */
	public static String zodiacByDate(Calendar time) {
		return zodiac[time.get(Calendar.YEAR) % 12];
	}

	/**
	 * 根据日期获取生肖
	 * 
	 * @param time
	 * @return String
	 */
	public static String zodiacByDate(Date time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);

		return zodiacByDate(calendar);
	}

	/**
	 * 根据日期获取星座
	 * 
	 * @param time
	 * @return String
	 */
	public static String constellation(Calendar time) {
		if (time == null) {
			return null;
		}
		int month = time.get(Calendar.MONTH);
		int day = time.get(Calendar.DAY_OF_MONTH);
		if (day < constellationEdgeDay[month]) {
			month = month - 1;
		}

		if (month >= 0) {
			return constellation[month];
		}

		return constellation[11];
	}

	/**
	 * 根据日期获取星座
	 * 
	 * @param time
	 * @return String
	 */
	public static String constellation(Integer time) {
		if (time == null || time.equals(0)) {
			return null;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		
		try {
			date = sdf.parse(time.toString());
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		return constellation(calendar);
	}

	/**
	 * 根据日期获取星座
	 * 
	 * @param time
	 * @return String
	 */
	public static String constellation(Date time) {
		if (time == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);

		return constellation(calendar);
	}
}
